package com.coffeeloversupportlibrary.coffeelover.mercury.my;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatButton;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.entity.share.Share;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.Game;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.User;
import com.coffeeloversupportlibrary.coffeelover.mercury.game.FirstStageActivity;
import com.coffeeloversupportlibrary.coffeelover.mercury.game.GameStartActivity;
import com.coffeeloversupportlibrary.coffeelover.mercury.game.SecondStageActivity;
import com.coffeeloversupportlibrary.coffeelover.mercury.game.ThirdStageActivity;
import com.coffeeloversupportlibrary.coffeelover.mercury.my.exchangerecord.ExChangeRecordActivity;
import com.coffeeloversupportlibrary.coffeelover.mercury.utils.HttpUtils;
import com.coffeeloversupportlibrary.coffeelover.mercury.utils.JsonUtils;

import org.apache.commons.httpclient.HttpConnection;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Set;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * @author 盛守义
 * @createTime 2021/11/3 17:07
 * @projectName TurningCoffee
 * @className SettingsActivity.java
 * @description TODO
 */
public class SettingsActivity extends Activity {
    private SharedPreferences userInfo;
    private Activity activity = SettingsActivity.this;

    private LinearLayout ll_userset;
    private ImageView iv_skip, civ_usermsgimg;
    private TextView tv_username, tv_sign;
    private RelativeLayout rl_safe, rl_address, rl_exrecord, rl_private, rl_univer, rl_model, rl_about;
    private AppCompatButton btn_exuser, btn_exit;

    private User user;
    private Intent intent;

    private JsonUtils jsonUtils = new JsonUtils();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity_set);


        initViews();
        setListener();
        setUserMsg();
    }

    void setUserMsg(){
        RequestOptions requestOptions = new RequestOptions().error(R.drawable.avatar).fallback(R.drawable.avatar);
        Glide.with(this).load(user.getData().getUser().getUserImg()).apply(requestOptions).into(civ_usermsgimg);
        tv_username.setText(user.getData().getUser().getUserNickname().trim());
        tv_sign.setText("咖啡豆数： "+user.getData().getUser().getCoffeeBean().toString().trim());

    }

    void setListener(){
        SetOnClickListener listener = new SetOnClickListener();

        rl_exrecord.setOnClickListener(listener);
        iv_skip.setOnClickListener(listener);

        rl_about.setOnClickListener(listener);
    }

    class SetOnClickListener implements View.OnClickListener{
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.iv_skipset:
                    finish();
                    break;
                case R.id.civ_usermsgset:
                    intent = new Intent(SettingsActivity.this, UserMessageActivity.class);
                    intent.putExtra("userInfo", user);
                    startActivity(intent);
                    break;
                case R.id.rl_exrecordset:
                    intent = new Intent(SettingsActivity.this, ExChangeRecordActivity.class);
                    startActivity(intent);
                    break;
                case R.id.rl_addressset:
                    startActivity(new Intent(SettingsActivity.this, AddressActivity.class));

                case R.id.btn_exuserset:
                    //切换账户
                    break;
                case R.id.btn_exitset:
                    //退出登录
                    break;

                case R.id.rl_aboutset:
                    HttpUtils httpUtils = new HttpUtils();
                    JsonUtils jsonUtils = new JsonUtils();
                    SharedPreferences userInfo = getSharedPreferences("user", 0);
                    User user = jsonUtils.getUser(userInfo.getString("userInfo" ,""));
                    String userId = user.getData().getUser().getUserId();
                    //暂时是游戏
                    Call call = httpUtils.getCall("/tree/tree/"+userId);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(@NotNull Call call, @NotNull IOException e) {
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(activity, "网络连接崩溃了，绝对绝对不是我们的错", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                        @Override
                        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                            String res = response.body().string();
                            Log.e("Main", "onResponse: " + res);
                            Game game = jsonUtils.getGame(res);
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    SharedPreferences.Editor editor = userInfo.edit();
                                    editor.putString("tree", res);
                                    editor.commit();

                                    if (game.getData().getTree() == null) {
                                        Intent intent = new Intent(activity, GameStartActivity.class);
                                        startActivity(intent);
                                    } else{
                                        Intent intent = new Intent(activity, FirstStageActivity.class);
                                        startActivity(intent);
                                    }
                                }
                            });
                        }
                    });
                    break;
            }
        }
    }

    void initViews(){
        civ_usermsgimg = findViewById(R.id.civ_usermsgset);
        iv_skip = findViewById(R.id.iv_skipset);
        ll_userset = findViewById(R.id.ll_userset);
        tv_sign = findViewById(R.id.tv_signset);
        tv_username = findViewById(R.id.tv_usernameset);
        rl_about = findViewById(R.id.rl_aboutset);
        rl_address = findViewById(R.id.rl_addressset);
        rl_univer = findViewById(R.id.rl_universalset);
        rl_safe = findViewById(R.id.rl_safeset);
        rl_exrecord = findViewById(R.id.rl_exrecordset);
        rl_model = findViewById(R.id.rl_modelset);
        rl_private = findViewById(R.id.rl_privateset);
        btn_exuser = findViewById(R.id.btn_exuserset);
        btn_exit = findViewById(R.id.btn_exitset);

        userInfo = getSharedPreferences("user", 0);
        Log.e("Settings", "initViews: "+userInfo.getString("userInfo", ""));
        user = jsonUtils.getUser(userInfo.getString("userInfo",""));
    }
}
